<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <search ref='search' :config='searchConfig' @search='search' @getHeight='getHeight' />

    <!-- table区域-begin -->
    <div class='page_table'>
      <vxe-grid
        ref='xGrid'
        :border='true'
        :resizable='true'
        :showOverflow='true'
        size='mini'
        :loading='loading'
        :height='pageHeight-seachHeight-90'
        :align='left'
        :columns='columns'
        :data='dataSource'
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :toolbar-config='tableToolbar'
        :checkbox-config="checkboxConfig"
        v-bind='vexConfig'
        @checkbox-change='selectChangeEvent'
        @checkbox-all='selectChangeEvent'
      >
        <template #toolbar_buttons>
          <div class='table-operator'>
<!--            <a-button type='primary' @click='handleAdd'>按线路组新增</a-button>-->
            <!-- <a-button @click="handle(row)" type='primary' v-if='!disableSubmit'>按线路组新增</a-button> -->
<!--            <a-button type='primary' @click="handleConfig(row)">新增</a-button>-->
            <a-button type='primary' @click="batchDel">删除</a-button>
<!--            <a-button @click="handleConfigTer(row)" type='primary' v-if='!disableSubmit'>按频率新增</a-button>-->
            <!-- <a-button type='primary' @click="batchDel">删除</a-button> -->
            <a-button type="primary" @click="downExportXls('visit_line')">导出</a-button>
          </div>
        </template>

        <template #status='{ row }'>
          <a-switch :checked='row.status' :disabled="row.visitStatus!==1" size='small' @change='handleStatus(row,$event)' />
        </template>
        <template #action='{ row:record }'>
          <a @click='handleEdit1(record)'>编辑</a>
          <span v-if='record.fileUrl'>
            <a-divider type='vertical' />
            <a :href='record.fileUrl'>下载</a>
          </span>
        </template>
        <template #pager>
          <vxe-pager
            :layouts="['Total', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump','Sizes', 'FullJump']"
            :currentPage.sync='ipagination.current'
            :page-size.sync='ipagination.pageSize'
            :total='ipagination.total'
            :pageSizes='ipagination.pageSizeOptions'
            @page-change='handlePageChange'>
          </vxe-pager>
        </template>
      </vxe-grid>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <!-- <sfaVisitLineEntity-modal ref="modalForm" @ok="modalFormOk"></sfaVisitLineEntity-modal> -->
    <SfaVisitLineList-modal ref="modalForm" @ok="modalFormOk"></SfaVisitLineList-modal>
    <!--    选择对应网点-->
<!--    <JVisitClientModal-->
<!--      modal-width="90%"-->
<!--      ref="JVisitClientModal"-->
<!--     >-->
<!--    </JVisitClientModal>-->
    <!--    选择对线路组-->
    <JVisitGroupModal
      modal-width="90%"
      ref="JVisitGroupModal"
      @ok="handelSave">
    </JVisitGroupModal>
    <VisitConfigModal
      modal-width="90%"
      ref="VisitConfigModal"
      @ok="handelSaveOk">
    </VisitConfigModal>
  </a-card>
</template>

<script>
  import '@/assets/less/TableExpand.less'
  import SfaVisitLineListModal from './modules/SfaVisitLineListModal'
  import { PageListMixin } from '@/mixins/PageListMixin'
  import moment from 'moment'
  import JSelectUserPosition from '../../../components/cn-select-popup/JSelectUserPosition'
  import { postAction } from '../../../api/manage'
  // import JVisitClientModal from './modules/JVisitClientModal'
  import JVisitGroupModal from './modules/JVisitGroupModal'

  import pick from 'lodash.pick'
  export default {
    name: 'SfaVisitLineList',
    mixins: [PageListMixin],
    components: {
      SfaVisitLineListModal,
      JSelectUserPosition,
      // JVisitClientModal,
      JVisitGroupModal
    },
    data() {
      return {
        description: '拜访明细查询报表',
        operType: 0,
        //查询区域
        searchConfig:[
          {
            type: 'input',
            name: 'visitUserRealName',
            placeholder: '拜访人名'
          },
          {
            type: 'input',
            name: 'clientCode',
            placeholder: '网点编码'
          },
          {
            type: 'input',
            name: 'clientName',
            placeholder: '网点名称'
          },
          {
            type: 'date-range', //范围选择
            range: ['visitDateStart', 'visitDateEnd'],
            // name: 'tempVisitDate',
            format: 'YYYY-MM-DD',
            placeholder: ['拜访开始时间', '拜访结束时间']
          },
          // {
          //   type: 'date',
          //   name: 'visitDateEnd',
          //   placeholder: '请选择日期'
          // },
        ],
        // 表头
        columns: [
          { type: 'checkbox', width: 50, fixed: 'left' },
          // {
          //   title: '优先级',
          //   minWidth:80,
          //   field: 'priority'
          // },
          {
            title: '网点类型',
            minWidth:80,
            field: 'clientTypeStr'
          },
          {
            title: '网点编码',
            minWidth:80,
            field: 'clientCode'
          },
          {
            title: '网点名称',
            minWidth:80,
            field: 'clientName'
          },
          {
            title: '拜访人',
            minWidth:80,
            field: 'visitUserName'
          },
          {
            title: '拜访人名',
            minWidth:80,
            field: 'visitUserRealName'
          },
          {
            title: '拜访人职位',
            minWidth:100,
            field: 'visitPosCode'
          },
          {
            title: '拜访人职位名',
            minWidth:120,
            field: 'visitPosName'
          },
          {
            title: '拜访开始日期',
            minWidth:120,
            field: 'visitDateStart'
          },
          {
            title: '拜访结束日期',
            minWidth:120,
            field: 'visitDateEnd'
          },
          {
            title: '拜访类型',
            minWidth:80,
            field: 'visitTypeStr'
          },
          {
            title: '拜访状态',
            minWidth:80,
            field: 'visitStatusStr'
          },
          {
            title: '创建人',
            minWidth:80,
            field: 'createName'
          },
          {
            title: '创建时间',
            minWidth:80,
            field: 'createTime'
          },
          {
            title: '状态',
            minWidth:80,
            field: 'status',
            slots:{default:'status'}
          },

        ],
        isorter: {
          permflag: 'visit_detail_page_impl'
        },
        url: {
          list: window._CONFIG['domianSfaURL'] + '/visit/sfaVisitLineController/findPageList',
          delete: window._CONFIG['domianSfaURL'] + '/visit/sfaVisitLineController/delete',
          deleteBatch: window._CONFIG['domianSfaURL'] + '/visit/sfaVisitLineController/deleteBatch',
          startOrStopUrl:window._CONFIG['domianSfaURL'] + '/visit/sfaVisitLineController/updateStatus',
          exportXlsUrl: window._CONFIG['domianSfaURL'] + 'visit/sfaVisitLineController/exportXls',
          importExcelUrl: window._CONFIG['domianSfaURL'] + 'visit/sfaVisitLineController/importExcel'
        }
      }
    },
    computed: {
      importExcelUrl: function() {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
      }
    },

    methods: {
      //限制当天之前的日期不可选
      disabledDate(current) {
        // return current && current <moment().subtract(1, "days"); //当天之前的不可选，不包括当天
        return current && current < moment().endOf('day') //当天之前的不可选，包括当天
      },
      handle(record, type, modal = 'SfaVisitLineListModal') {
        console.log(this.$refs[modal])
        this.$refs[modal].showModal(record)
        return false
      },
      // handleConfigTer(record, type, modal = 'JVisitGroupModal') {
      //   this.$refs[modal].showModal(record)
      //   return false
      // },
      // handleConfig(record, type, modal = 'JVisitClientModal') {
      //   this.$refs[modal].showModal(record)
      //   return false
      // },
      searchQuery() {
        if (this.checkParam(false)) {
          this.loadData(1)
        }
      },
      handleAddLineByGroup() {
        if (this.checkParam(true)) {
          this.operType = 2
          this.$refs.JVisitGroupModal.showModal(this.queryParam.visitPosCode)
        }
      },
      handleAddLine() {
        if (this.checkParam(true)) {
          this.operType = 1
          this.$refs.JVisitClientModal.showModal(this.queryParam.visitPosCode)
        }

      },
      checkParam(ischeck) {
        if (ischeck) {
          if (!this.queryParam.visitPosCode) {
            this.$message.warning('请选择拜访人')
            return false
          }
          if (!this.queryParam.visitDates || this.queryParam.visitDates.length == 0) {
            this.$message.warning('请选择拜访时间')
            return false
          }
          let temp = []
          this.queryParam.visitDates.map(row => {
            temp.push(moment(row).format('YYYY-MM-DD'))
          })
          this.queryParam.visitDates = temp
        } else {
          // this.queryParam.tempVisitDate = moment(this.queryParam.tempVisitDate).format('YYYY-MM-DD')
        }
        return true
      },
      handelSave(clients, groupCode) {
        const record = {
          visitPosCode: this.queryParam.visitPosCode,
          visitDates: this.queryParam.visitDates,
          operType: this.operType
        }
        if (this.operType === 1) {
          record.lineVos = clients
        } else {
          record.groups = clients
        }
        var that = this
        console.log(record)
        postAction(window._CONFIG['domianSfaURL'] + '/visit/sfaVisitLineController/saveOrUpdate', record).then(async (res) => {
          if (res.success) {
            that.$message.success(res.message)
            await this.loadData()
            if (this.operType === 1) {
              this.$refs.JVisitClientModal.close()
            } else {
              this.$refs.JVisitGroupModal.close()
            }
          } else {
            this.$message.warning(res.message)
          }
        }).finally(() => {
          if (this.operType === 1) {
            this.$refs.JVisitClientModal.confirmLoading = false
          } else {
            this.$refs.JVisitGroupModal.confirmLoading = false
          }
        })
      },
      async handleClientModal(record) {
        if (this.selectedRowKeys.length != 1) {
          this.$message.warning('请选择一条记录！')
          return
        }
        this.$refs.JSelectClientModal.showModal(this.selectionRows[0].groupCode)
      },
      handleAddUser() {
          this.$refs.VisitConfigModal.showModal()
      },
      handelSaveOk(pos){
         if (pos!=null && pos !=null){
           this.$set(this.queryParam,"visitPosCode",pos.positionCode);
           this.$set(this.queryParam,"queryVisitPosName",pos.positionName+"-"+pos.realname);
         }
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>